import 'package:flutter/material.dart';
import 'package:getwidget/getwidget.dart';
import 'package:ichinesereader/pages/common/myWords.dart';
import 'package:ichinesereader/widgets/dialogs/newOrEditWordFolderDialog.dart';
import 'package:ichinesereader/widgets/dialogs/deleteWordDialog.dart';
import 'package:ichinesereader/controller/knowledgeword.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/widgets/dialogs/selectFolderDialog.dart';
import 'package:ichinesereader/widgets/dialogs/selectStudentsDialog.dart';
import 'package:ichinesereader/widgets/dialogs/createWordCardDialog.dart';
import 'package:ichinesereader/models/knowledgeword/knowledgeWord.dart';
import '../../constants.dart';
import '../dialogs/wordCardSettingDialog.dart';

class WordButtonBar extends GetView<KnowledgeWordsController> {
  final bool isTeacher;

  WordButtonBar({Key? key, required this.isTeacher}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final isSmallScreen =
        MediaQuery.of(context).size.width < Constants.DefinedScreenSmallSize;
    return isSmallScreen
        ? Column(
            children: <Widget>[
              Obx(() {
                List<Widget> children = [];
                if (controller.mainMode.value) {
                  for (int i = 0; i < controller.currDirName.length; i++) {
                    String? name = controller.currDirName[i];
                    children.add(Padding(
                      padding: const EdgeInsets.only(left: 12),
                      child: InkWell(
                        child: Text(
                          "$name/",
                          style: TextStyle(
                              fontFamily: '',
                              color: Get.isDarkMode
                                  ? Get.theme.primaryColorDark
                                  : Colors.black87,
                              fontSize: 15.0),
                        ),
                        onTap: () {
                          controller.changeDir(i);
                        },
                      ),
                    ));
                  }
                }
                return Row(
                  // child: Text(text),
                  children: children,
                );
              }),
              Padding(
                padding: const EdgeInsets.only(top: 12.0),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    Visibility(
                        visible: controller.mainMode.value,
                        child: Padding(
                          padding: const EdgeInsets.only(left: 6.0),
                          child: GFIconButton(
                            color: Theme.of(context).primaryColorDark,
                            onPressed: () {
                              showGeneralDialog(
                                  context: context,
                                  transitionDuration:
                                      Duration(milliseconds: 400),
                                  pageBuilder: (bc, ania, anis) {
                                    return NewOrEditWordFolderDialog(
                                        title: isTeacher
                                            ? 'Create Lesson Plan'.tr
                                            : 'Create Folder'.tr,
                                        message: '',
                                        type: 'create');
                                  });
                            },
                            icon: Icon(
                              Icons.create_new_folder_outlined,
                              color: Get.isDarkMode
                                  ? Colors.black
                                  : Theme.of(context).primaryColorLight,
                            ),
                            tooltip: isTeacher
                                ? 'Create Lesson Plan'.tr
                                : 'Create Folder'.tr,
                            size: GFSize.SMALL,
                          ),
                        )),
                    Visibility(
                        visible: isTeacher && controller.mainMode.value,
                        child: Padding(
                          padding: const EdgeInsets.only(left: 6.0),
                          child: GFIconButton(
                            color: Theme.of(context).primaryColorDark,
                            onPressed: () {
                              showGeneralDialog(
                                  context: context,
                                  transitionDuration:
                                      Duration(milliseconds: 400),
                                  pageBuilder: (bc, ania, anis) {
                                    return CreateWordCardDialog(
                                      title: 'Create Word Card'.tr,
                                      wordImageAddress: '',
                                      type: 'create',
                                      word: KnowledgeWord(),
                                    );
                                  });
                            },
                            tooltip: 'Create Word Card'.tr,
                            icon: Icon(
                              Icons.create_sharp,
                              color: Get.isDarkMode
                                  ? Colors.black
                                  : Theme.of(context).primaryColorLight,
                            ),
                            size: GFSize.SMALL,
                          ),
                        )),
                    Visibility(
                        visible: controller.mainMode.value,
                        child: Padding(
                          padding: const EdgeInsets.only(left: 6.0),
                          child: GFIconButton(
                            color: Theme.of(context).primaryColorDark,
                            onPressed: () {
                              showGeneralDialog(
                                  context: context,
                                  transitionDuration:
                                      Duration(milliseconds: 400),
                                  pageBuilder: (bc, ania, anis) {
                                    return DeleteWordDialog(
                                        title: '',
                                        message:
                                            'Are you sure you want to '.tr);
                                  });
                            },
                            tooltip: isTeacher
                                ? 'Delete Lesson Plan or Word Card'.tr
                                : 'Delete Folder or Word Card'.tr,
                            icon: Icon(
                              Icons.delete_outlined,
                              color: Get.isDarkMode
                                  ? Colors.black
                                  : Theme.of(context).primaryColorLight,
                            ),
                            size: GFSize.SMALL,
                          ),
                        )),
                    Visibility(
                      visible: isTeacher && controller.mainMode.value,
                      child: Padding(
                        padding: const EdgeInsets.only(left: 6.0),
                        child: GFIconButton(
                          color: Theme.of(context).primaryColorDark,
                          onPressed: () {
                            showGeneralDialog(
                                context: context,
                                transitionDuration: Duration(milliseconds: 400),
                                pageBuilder: (bc, ania, anis) {
                                  return SelectStudentsDialog(
                                      title: 'Share to Students'.tr);
                                });
                          },
                          tooltip: 'Share to Students'.tr,
                          icon: Icon(
                            Icons.my_library_add_outlined,
                            color: Get.isDarkMode
                                ? Colors.black
                                : Theme.of(context).primaryColorLight,
                          ),
                          size: GFSize.SMALL,
                        ),
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.only(left: 6.0),
                      child: GFIconButton(
                        color: Theme.of(context).primaryColorDark,
                        onPressed: () {
                          showGeneralDialog(
                              context: context,
                              transitionDuration: Duration(milliseconds: 400),
                              pageBuilder: (bc, ania, anis) {
                                if (controller.mainMode.value) {
                                  return SelectFolderDialog(
                                      title: 'Folder Or Word Card move',
                                      fromFolderId: controller.currdir[
                                              controller.currdir.length - 1]
                                          .toString(),
                                      from: 'myWords');
                                } else {
                                  return SelectFolderDialog(
                                      title: 'Folder Or Word Card move',
                                      fromFolderId:
                                          controller.recycleFolderId.toString(),
                                      from: 'myWords');
                                }
                              });
                        },
                        tooltip: isTeacher
                            ? 'Move to Lesson Plan'.tr
                            : 'Move to Folder'.tr,
                        icon: Icon(
                          // Icons.remove_circle_outline,
                          Icons.move_to_inbox,
                          color: Get.isDarkMode
                              ? Colors.black
                              : Theme.of(context).primaryColorLight,
                        ),
                        size: GFSize.SMALL,
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.only(left: 6.0),
                      child: GFIconButton(
                        color: Theme.of(context).primaryColorDark,
                        onPressed: () {
                          showGeneralDialog(
                              context: context,
                              transitionDuration: Duration(milliseconds: 400),
                              pageBuilder: (bc, ania, anis) {
                                return WordCardSettingDialog(
                                    title: 'Word Card Settings');
                              });
                        },
                        tooltip: 'Word Card Settings'.tr,
                        icon: Icon(
                          // Icons.remove_circle_outline,
                          Icons.settings,
                          color: Get.isDarkMode
                              ? Colors.black
                              : Theme.of(context).primaryColorLight,
                        ),
                        size: GFSize.SMALL,
                      ),
                    ),
                    Visibility(
                        visible: true,
                        child: Padding(
                          padding: const EdgeInsets.only(left: 6.0),
                          child: GFIconButton(
                            color: Theme.of(context).primaryColorDark,
                            onPressed: () {
                              if (wordCardKeys.isNotEmpty) {
                                disableWordCardAudioPlay = true;
                                wordCardKeys.forEach((cardkey) {
                                  cardkey.currentState!.flipCard();
                                });
                                disableWordCardAudioPlay = false;
                              }
                            },
                            tooltip: 'Flip Wordcard'.tr,
                            icon: Icon(
                              Icons.flash_auto,
                              color: Get.isDarkMode
                                  ? Colors.black
                                  : Theme.of(context).primaryColorLight,
                            ),
                            size: GFSize.SMALL,
                          ),
                        )),
                    Padding(
                      padding: const EdgeInsets.only(left: 6.0),
                      child: GFIconButton(
                        color: Theme.of(context).primaryColorDark,
                        onPressed: () {
                          controller.changeAllWordCardStatus();
                        },
                        tooltip: 'Select All or Unselect All'.tr,
                        icon: Icon(
                          Icons.select_all,
                          color: Get.isDarkMode
                              ? Colors.black
                              : Theme.of(context).primaryColorLight,
                        ),
                        size: GFSize.SMALL,
                      ),
                    ),
                  ],
                ),
              )
            ],
          )
        : Row(
            children: <Widget>[
              Expanded(child: Obx(() {
                List<Widget> children = [];
                if (controller.mainMode.value) {
                  for (int i = 0; i < controller.currDirName.length; i++) {
                    String? name = controller.currDirName[i];
                    children.add(Padding(
                      padding: const EdgeInsets.only(left: 12),
                      child: InkWell(
                        child: Text(
                          "$name/",
                          style: TextStyle(
                              fontFamily: '',
                              color: Get.isDarkMode
                                  ? Get.theme.primaryColorDark
                                  : Colors.black87,
                              fontSize: 15.0),
                        ),
                        onTap: () {
                          controller.changeDir(i);
                        },
                      ),
                    ));
                  }
                }
                return Row(
                  // child: Text(text),
                  children: children,
                );
              })),
              Visibility(
                  visible: controller.mainMode.value,
                  child: Padding(
                    padding: const EdgeInsets.only(left: 6.0),
                    child: GFIconButton(
                      color: Theme.of(context).primaryColorDark,
                      onPressed: () {
                        showGeneralDialog(
                            context: context,
                            transitionDuration: Duration(milliseconds: 400),
                            pageBuilder: (bc, ania, anis) {
                              return NewOrEditWordFolderDialog(
                                  title: isTeacher
                                      ? 'Create Lesson Plan'.tr
                                      : 'Create Folder'.tr,
                                  message: '',
                                  type: 'create');
                            });
                      },
                      icon: Icon(
                        Icons.create_new_folder_outlined,
                        color: Get.isDarkMode
                            ? Colors.black
                            : Theme.of(context).primaryColorLight,
                      ),
                      tooltip: isTeacher
                          ? 'Create Lesson Plan'.tr
                          : 'Create Folder'.tr,
                      size: GFSize.SMALL,
                    ),
                  )),
              Visibility(
                  visible: isTeacher && controller.mainMode.value,
                  child: Padding(
                    padding: const EdgeInsets.only(left: 6.0),
                    child: GFIconButton(
                      color: Theme.of(context).primaryColorDark,
                      onPressed: () {
                        showGeneralDialog(
                            context: context,
                            transitionDuration: Duration(milliseconds: 400),
                            pageBuilder: (bc, ania, anis) {
                              return CreateWordCardDialog(
                                title: 'Create Word Card'.tr,
                                wordImageAddress: '',
                                type: 'create',
                                word: KnowledgeWord(),
                              );
                            });
                      },
                      tooltip: 'Create Word Card'.tr,
                      icon: Icon(
                        Icons.create_sharp,
                        color: Get.isDarkMode
                            ? Colors.black
                            : Theme.of(context).primaryColorLight,
                      ),
                      size: GFSize.SMALL,
                    ),
                  )),
              Visibility(
                  visible: controller.mainMode.value,
                  child: Padding(
                    padding: const EdgeInsets.only(left: 6.0),
                    child: GFIconButton(
                      color: Theme.of(context).primaryColorDark,
                      onPressed: () {
                        showGeneralDialog(
                            context: context,
                            transitionDuration: Duration(milliseconds: 400),
                            pageBuilder: (bc, ania, anis) {
                              return DeleteWordDialog(
                                  title: '',
                                  message: 'Are you sure you want to '.tr);
                            });
                      },
                      tooltip: isTeacher
                          ? 'Delete Lesson Plan or Word Card'.tr
                          : 'Delete Folder or Word Card'.tr,
                      icon: Icon(
                        Icons.delete_outlined,
                        color: Get.isDarkMode
                            ? Colors.black
                            : Theme.of(context).primaryColorLight,
                      ),
                      size: GFSize.SMALL,
                    ),
                  )),
              Visibility(
                visible: isTeacher && controller.mainMode.value,
                child: Padding(
                  padding: const EdgeInsets.only(left: 6.0),
                  child: GFIconButton(
                    color: Theme.of(context).primaryColorDark,
                    onPressed: () {
                      showGeneralDialog(
                          context: context,
                          transitionDuration: Duration(milliseconds: 400),
                          pageBuilder: (bc, ania, anis) {
                            return SelectStudentsDialog(
                                title: 'Share to Students'.tr);
                          });
                    },
                    tooltip: 'Share to Students'.tr,
                    icon: Icon(
                      Icons.my_library_add_outlined,
                      color: Get.isDarkMode
                          ? Colors.black
                          : Theme.of(context).primaryColorLight,
                    ),
                    size: GFSize.SMALL,
                  ),
                ),
              ),
              Padding(
                padding: const EdgeInsets.only(left: 6.0),
                child: GFIconButton(
                  color: Theme.of(context).primaryColorDark,
                  onPressed: () {
                    showGeneralDialog(
                        context: context,
                        transitionDuration: Duration(milliseconds: 400),
                        pageBuilder: (bc, ania, anis) {
                          if (controller.mainMode.value) {
                            return SelectFolderDialog(
                                title: 'Folder Or Word Card move',
                                fromFolderId: controller
                                    .currdir[controller.currdir.length - 1]
                                    .toString(),
                                from: 'myWords');
                          } else {
                            return SelectFolderDialog(
                                title: 'Folder Or Word Card move',
                                fromFolderId:
                                    controller.recycleFolderId.toString(),
                                from: 'myWords');
                          }
                        });
                  },
                  tooltip: isTeacher
                      ? 'Move to Lesson Plan'.tr
                      : 'Move to Folder'.tr,
                  icon: Icon(
                    // Icons.remove_circle_outline,
                    Icons.move_to_inbox,
                    color: Get.isDarkMode
                        ? Colors.black
                        : Theme.of(context).primaryColorLight,
                  ),
                  size: GFSize.SMALL,
                ),
              ),
              Padding(
                padding: const EdgeInsets.only(left: 6.0),
                child: GFIconButton(
                  color: Theme.of(context).primaryColorDark,
                  onPressed: () {
                    showGeneralDialog(
                        context: context,
                        transitionDuration: Duration(milliseconds: 400),
                        pageBuilder: (bc, ania, anis) {
                          return WordCardSettingDialog(
                              title: 'Word Card Settings');
                        });
                  },
                  tooltip: 'Word Card Settings'.tr,
                  icon: Icon(
                    // Icons.remove_circle_outline,
                    Icons.settings,
                    color: Get.isDarkMode
                        ? Colors.black
                        : Theme.of(context).primaryColorLight,
                  ),
                  size: GFSize.SMALL,
                ),
              ),
              Visibility(
                  visible: true,
                  child: Padding(
                    padding: const EdgeInsets.only(left: 6.0),
                    child: GFIconButton(
                      color: Theme.of(context).primaryColorDark,
                      onPressed: () {
                        if (wordCardKeys.isNotEmpty) {
                          disableWordCardAudioPlay = true;
                          wordCardKeys.forEach((cardkey) {
                            cardkey.currentState!.flipCard();
                          });
                          disableWordCardAudioPlay = false;
                        }
                      },
                      tooltip: 'Flip Wordcard'.tr,
                      icon: Icon(
                        Icons.flash_auto,
                        color: Get.isDarkMode
                            ? Colors.black
                            : Theme.of(context).primaryColorLight,
                      ),
                      size: GFSize.SMALL,
                    ),
                  )),
              Padding(
                padding: const EdgeInsets.only(left: 6.0),
                child: GFIconButton(
                  color: Theme.of(context).primaryColorDark,
                  onPressed: () {
                    controller.changeAllWordCardStatus();
                  },
                  tooltip: 'Select All or Unselect All'.tr,
                  icon: Icon(
                    Icons.select_all,
                    color: Get.isDarkMode
                        ? Colors.black
                        : Theme.of(context).primaryColorLight,
                  ),
                  size: GFSize.SMALL,
                ),
              ),
            ],
          );
  }
}
